package com.ejuornal.action;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.ejuornal.bean.Permission;
import com.ejuornal.bean.RolePermission;
import com.ejuornal.bean.vo.PermissionVO;
import com.ejuornal.service.PermissionService;
import com.ejuornal.service.RolePermissionService;
import com.ejuornal.util.StrToIntArray;
@Component("permissionAction")
@Scope("prototype")
public class PermissionAction extends BaseAction<Permission>{
    private static final long serialVersionUID = -4923718133832686139L;
    private PermissionService permissionService;
    private Permission permission;
    private int pgId;
    private List<Permission> permissions;
    private int roleId;
    private RolePermission rp;
    private List<PermissionVO> pv;
    private RolePermissionService rps;
    private String pids;
    private String id;

    public String delRP() {
        int pid = Integer.parseInt(super.getRequest().getParameter("pid"));
        pgId = pid;
        if(rps.delete(pid) == true) {
            return SUCCESS;
        }
        return ERROR;
    }
    
    public String menuList() {
    	if(roleId == 0) {
    		return "error_adminLogin";
    	}
    	pv = permissionService.findPermissionVO(roleId);
    	
    	return SUCCESS;
    }
    
	public String listByRole() {
    	String roleId1 = (String) super.getRequestParameter("id");
    	
    	roleId = Integer.parseInt(roleId1);
    	permissions = permissionService.findByRoleId(roleId);
    	return SUCCESS;
    }
    
    public String rpAdd_1() {
    	roleId = getRequestParameter();
    	if(roleId == -1)  {
    		return ERROR;
    	}
    	permissions = permissionService.findByNotIntRole(roleId);
    	return SUCCESS;
    }
    
    public String rpAdd_2() {
    	int[] pp = StrToIntArray.ChangeStrToIntArray(pids);
    	for(int i=0; i < pp.length; i++) {
    		rp.setPid(pp[i]);
    		if(permissionService.addRolePermission(rp) == false) {
    		    return ERROR;
    		}
    	}
    	roleId = rp.getRoleId();
    	id = String.valueOf(roleId);
        return SUCCESS;
    }
    
    public String list() {
        int pgId = Integer.parseInt(super.getRequest().getParameter("pgId"));
        String url="admin/permissionList";
        pages = permissionService.find((page - 1) * 10, url, pgId);
        super.getRequest().setAttribute("id", pgId);
        return SUCCESS;
    }
    
    public String add() {
        permissionService.add(permission);
        pgId = permission.getPgId();
        return SUCCESS;
    }
    
    public String delete() {
        int pid = Integer.parseInt(super.getRequest().getParameter("pid"));
        pgId = pid;
        id = super.getRequest().getParameter("id");
        if(permissionService.delete(pid) == true) {
            return SUCCESS;
        }
        return ERROR;
    }
    
    public PermissionService getPermissionService() {
        return permissionService;
    }
    
    @Resource(name="permissionService")
    public void setPermissionService(PermissionService permissionService) {
        this.permissionService = permissionService;
    }

    public Permission getPermission() {
        return permission;
    }

    public void setPermission(Permission permission) {
        this.permission = permission;
    }

	public int getPgId() {
		return pgId;
	}

	public void setPgId(int pgId) {
		this.pgId = pgId;
	}


	public List<Permission> getPermissions() {
		return permissions;
	}


	public void setPermissions(List<Permission> permissions) {
		this.permissions = permissions;
	}
    
	 public int getRoleId() {
			return roleId;
		}

		public void setRoleId(int roleId) {
			this.roleId = roleId;
		}
		public RolePermission getRp() {
			return rp;
		}

		public void setRp(RolePermission rp) {
			this.rp = rp;
		}
		public List<PermissionVO> getPv() {
			return pv;
		}

		public void setPv(List<PermissionVO> pv) {
			this.pv = pv;
		}

		 public String getPids() {
		        return pids;
		    }


		    public void setPids(String pids) {
		        this.pids = pids;
		    }
		    public RolePermissionService getRps() {
		        return rps;
		    }

		    @Resource(name="rolePermissionService")
		    public void setRps(RolePermissionService rps) {
		        this.rps = rps;
		    }
		    public String getId() {
		        return id;
		    }


		    public void setId(String id) {
		        this.id = id;
		    }
}
